<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Group extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'sys_groups';

		$this->primary_key = 'sys_groups.group_id';

	}
	
	function get_group(){
		$this->load->database();					
			$sql = "SELECT * FROM sys_groups ORDER BY group_id;";
			$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	function get_group_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM sys_groups WHERE group_id='$id';";
		//$sql = "SELECT sys_users.*,sys_groups.name FROM sys_groups JOIN sys_users ON sys_groups.group_id=sys_users.group_id WHERE sys_users.user_id='$id' AND sys_users.group_id='$group_id' AND sys_users.pegawai_id='$pegawai_id'  ORDER BY sys_users.user_id; ";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	function get_group_session($id,$group_id){
		$this->load->database();		
		// $sql = "SELECT * FROM sys_groups WHERE group_id='$id';";
		 $sql = "SELECT sys_users.*,sys_groups.name FROM sys_groups JOIN sys_users ON sys_groups.group_id=sys_users.group_id WHERE sys_users.user_id='$id' AND sys_users.group_id='$group_id'  ORDER BY sys_users.user_id; ";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}	
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert('sys_groups',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where('group_id',$id);
				$this->db->update('sys_groups',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM sys_groups WHERE group_id='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>